/**
 *  Copyright (C) 2014-2015 The MeiRiQ All Right Reserve.
 */
package com.example.bannerdemo.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView.ScaleType;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.bannerdemo.bean.Banner;

/**
 * @version 1.0
 * @time 2015-6-8 下午8:55:38
 * @author zhangchaoxian@meiriq.com
 */
public class BannerAdapter extends PagerAdapter {

	public interface OnItemClickListener {
		/**
		 * 被点击了某个item
		 * 
		 * @param banner
		 */
		public void onItemClick(Banner banner);
	}

	private Context mContext = null;
	// 存放用于显示的view
	private List<View> mViews = null;
	// 存放展示的banner数据
	private List<Banner> mBanners = null;

	/** 图片加载 */
	private ImageLoader mImageLoader;
	private OnItemClickListener mItemClickListener;

	public BannerAdapter(Context context, List<Banner> list,
			OnItemClickListener listener) {
		mContext = context;
		mItemClickListener = listener;
		mImageLoader = VollyUtils.getImageLoader(context);

		mViews = new ArrayList<View>();
		this.mBanners = list;

		initView();
	}

	@Override
	public void notifyDataSetChanged() {
		// 清空原来的数据
		mViews.clear();
		initView();
		super.notifyDataSetChanged();
	}

	private void initView() {
		for (Banner banner : mBanners) {
			NetworkImageView imageView = new NetworkImageView(mContext);
			imageView.setScaleType(ScaleType.FIT_XY);
			imageView.setLayoutParams(new LayoutParams(-2, -2));
			imageView.setImageUrl(banner.getImg(), mImageLoader);
			imageView.setErrorImageResId(android.R.drawable.ic_delete);
			imageView.setDefaultImageResId(android.R.drawable.ic_secure);
			imageView.setOnClickListener(mClickListener);
			imageView.setTag(banner);

			mViews.add(imageView);
		}

	}

	private OnClickListener mClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (mItemClickListener != null) {
				mItemClickListener.onItemClick((Banner) v.getTag());
			}
		}
	};

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		// System.out.println("instantiateItem " + container + " pos=" +
		// position);
		View view = mViews.get(position);
		container.addView(view);
		return view;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		System.out.println("destroyItem " + container + "  pos=" + position
				+ "  obj=" + object);
		container.removeView(mViews.get(position));
	}

	@Override
	public int getCount() {
		if (mViews != null) {
			return mViews.size();
		}
		return 0;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// System.out.println("isViewFromObject " + (arg0 == arg1));
		return arg0 == arg1;
	}

	@Override
	public void finishUpdate(ViewGroup container) {
		super.finishUpdate(container);
		// System.out.println("finishUpdate " + container);
	}

	@Override
	public void startUpdate(ViewGroup container) {
		super.startUpdate(container);
		// System.out.println("startUpdate " + container);
	}

}
